/*
 *  Box2DDebugRenderer.h
 *  BugFix
 *
 *  Created by Dmitry Fadeev on 9/14/11.
 *  Copyright 2011 Arello Mobile. All rights reserved.
 *
 */

#pragma once

#include <physics/DebugRenderer.h>
#include <Box2d/Common/b2Draw.h>
#include <lib3d/hard/VertexBuffer.h>
#include <HardwareDependentObject.h>
#include <lib3d/hard/Manager.h>
#include <Color.h>
#include <math/Vector3.h>

namespace physics
{
	struct DebugVertices
	{
		DebugVertices(lib3d::hard::VertexBuffer * linesVB, lib3d::hard::VertexBuffer * trianglesVB);
		~DebugVertices();
		
		lib3d::hard::VertexBuffer * linesVB;
		lib3d::hard::VertexBuffer * trianglesVB;
	};
	
	class Box2DDebugRenderer : public DebugRenderer, public b2Draw, public HardwareDependentObject<lib3d::hard::Manager, DebugVertices>
	{
	private:
		struct DebugVertex
		{
			DebugVertex() : color(1.0f, 1.0f, 1.0f), alpha(1.0f)
			{
			}
			
			DebugVertex(float x, float y, float red, float green, float blue, float alpha) : position(x, y, 99.0f), color(red, green, blue), alpha(alpha)
			{
			}
			
			liba::math::Vector3<float> position;
			liba::Color                color;
			float                      alpha;
		};
	private:
		std::vector<DebugVertex> _linesList;
		std::vector<DebugVertex> _trianglesList;
	protected:
		virtual void on_add_hardware(lib3d::hard::Manager::hardware_type * hardware);
	public:
		Box2DDebugRenderer();
		virtual ~Box2DDebugRenderer();
		virtual void Clear();
		virtual void RenderToList(lib3d::hard::Hardware * hardware, lib3d::hard::RenderList * renderList);
		virtual void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);
		virtual void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);
		virtual void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color);
		virtual void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color);
		virtual void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color);
		virtual void DrawTransform(const b2Transform& xf);
	};
}